const { numberFormatter } = require('../src/numberFormatter')

const FORMATE_COMMON = 1;
const FORMATE_COMMON_CHINESE = 2;

const FORMATE_YEAR_MONTH = 4;
const FORMATE_YEAR_MONTH_CHINESE = 5;
const FORMATE_YEAR_MONTH_DOT = 6;

// 格式化时间戳
const dateFormatter = (timestamp, type) => {
  if (!timestamp) return "";

  try {
    let date = new Date(timestamp);
    let y = date.getFullYear();
    let m = numberFormatter(date.getMonth() + 1);
    let d = numberFormatter(date.getDate());

    switch (type) {
      case FORMATE_YEAR_MONTH:
        return y + "-" + m;

      case FORMATE_YEAR_MONTH_CHINESE:
        return y + "年" + m + "月";

      case FORMATE_YEAR_MONTH_DOT:
        return y + "." + m;

      case FORMATE_COMMON_CHINESE:
        return y + "年" + m + "月" + d + "日";

      case FORMATE_YEAR_MONTH_DOT:
        return y + "." + m + "." + d + ".";

      case FORMATE_COMMON:

      default:
        return y + "-" + m + "-" + d;
    }
  } catch (e) {
    console.error(e);
    return timestamp;
  }
};

// 获取开始时间
const startTime = (time) => {
  const nowTimeDate = new Date(time);
  return nowTimeDate.setHours(0, 0, 0, 0);
};

// 获取结束时间
const endTime = (time) => {
  const nowTimeDate = new Date(time);
  return nowTimeDate.setHours(23, 59, 59, 999);
};

/**
 * 获取当天开始时间和结束时间
 *
 * 2024.07.09 00:00:00 ~ 2024.09.09 23:59:59
 * @param {number | string} time
 * @returns
 */
const getTodayStartTimeAndEndTime = (time) => {
  time = time ? time : new Date();
  return {
    startTime: new Date(time.setHours(0, 0, 0, 0)).getTime(),
    endTime: new Date(time.setHours(23, 59, 59, 999)).getTime(),
  };
};
// 获得本周开始时间和结束时间
function getCurrentWeekStartTimeAndEndTime(time) {
  const current = time ? new Date(time) : new Date();
  // current是本周的第几天
  let nowDayOfWeek = current.getDay();
  if (nowDayOfWeek === 0) nowDayOfWeek = 7;
  const dayNum = 1 * 24 * 60 * 60 * 1000;
  // 获取本周星期一的时间，星期一作为一周的第一天
  const firstDate = new Date(current.valueOf() - (nowDayOfWeek - 1) * dayNum);
  // 获取本周星期天的时间，星期天作为一周的最后一天
  const lastDate = new Date(new Date(firstDate).valueOf() + 6 * dayNum);
  return {
    startTime: new Date(startTime(firstDate)).getTime(),
    endTime: new Date(endTime(lastDate)).getTime(),
  };
}
// 获得本月第一天
function getCurrentMonthFirst(time) {
  const date = time ? new Date(time) : new Date();
  date.setDate(1);
  return startTime(date);
}
// 获得本月最后一天
function getCurrentMonthLast(time) {
  const date = time ? new Date(time) : new Date();
  const currentMonth = date.getMonth();
  const nextMonth = currentMonth + 1;
  const nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
  const oneDay = 24 * 60 * 60 * 1000;
  return endTime(new Date(nextMonthFirstDay - oneDay));
}
// 本年第一天
function getCurrentYearFirst(date) {
  date = date ? new Date(date) : new Date();
  date.setDate(1);
  date.setMonth(0);
  return startTime(date);
}
// 本年最后一天
function getCurrentYearLast(date) {
  date = date ? new Date(date) : new Date();
  date.setFullYear(date.getFullYear() + 1); // 设置到明年
  date.setMonth(0); // 明年的0月，也就是对应到1月，是存在的哦，不是不存在的0
  date.setDate(0); // 明年的0日
  return endTime(date);
}

/**
 * 获得某天是第几周
 */
const weeks = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
function getWeekChinese(datetimes) {
  return weeks[new Date(Number(datetimes) * 1000).getDay()];
}

/**
 * 添加天数
 */
function addDays(date, days) {
  let result = new Date(date);
  result.setDate(result.getDate() + days);
  return result;
}

/**
 * 添加月数
 */
function addMonths(date, months) {
  let result = new Date(date);
  result.setMonth(result.getMonth() + months);
  return result;
}

/**
 * 添加年数
 */
function addYears(date, years) {
  let result = new Date(date);
  result.setFullYear(result.getFullYear() + years);
  return result;
}

/**
 * 减少天数
 */
function subtractDays(date, days) {
  let result = new Date(date);
  result.setDate(result.getDate() - days);
  return result;
}

/**
 * 减少月数
 */
function subtractMonths(date, months) {
  let result = new Date(date);
  result.setMonth(result.getMonth() - months);
  return result;
}

/**
 * 减少年数
 */
function subtractYears(date, years) {
  let result = new Date(date);
  result.setFullYear(result.getFullYear() - years);
  return result;
}


(function() {
   console.log(dateFormatter(new Date()))
})()